package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.Hotelroomitem;
import com.common.entity.PageParam;
import com.common.entity.Programme;
import com.common.entity.Rsp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.ProgrammeMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class ProgrammeService implements BaseService<Programme> {
    @Resource
    ProgrammeMapper programmeMapper;


    @Override
    public PageInfo<Programme> searchSomeByPage(PageParam<Programme> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Programme> newscategoryList=programmeMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Programme> newshotelPageInfo = new PageInfo<Programme>(newscategoryList);
            return newshotelPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Programme> programmes = programmeMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Programme> orderPageInfo = new PageInfo<Programme>(programmes);
            return orderPageInfo;
        }
    }

    @Override
    public Programme getOneById(String id) {
        Programme oneById = programmeMapper.getOneById(id);
        if (!StringUtils.isEmpty(oneById)) {
            return oneById;
        }
        return null;
    }

    @Override
    public Rsp addOne(Programme programme) {
        programme.setId(UUID.randomUUID().toString().replace("-",""));
        int i = programmeMapper.addOne(programme);
        if(i>0){
            return Rsp.success(programme) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteOneTrue(String id) {
        return null;
    }

    @Override
    public Rsp update(Programme programme) {
        int i = programmeMapper.update(programme);
        if(i>0){
            return Rsp.success(programmeMapper.getOneById(programme.getId())) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(Programme programme) {

        return null;
    }


    @Override
    public Rsp deleteSomeLogicalByCondition(Programme programme) {
        programme.setDeleted(1);
        int i = programmeMapper.update(programme);
        if(i>0){
            return Rsp.success(programmeMapper.getOneById(programme.getId())) ;
        }
        return Rsp.fail();
    }

    //查询所有方案
    public List<Programme> searchAllProgramme(){
        List<Programme> programmes = programmeMapper.programmeMapper();
        if(!StringUtils.isEmpty(programmes)){
            return programmes;
        }else {
            return null;
        }

    }
}
